import { Injectable } from "@nestjs/common";
import { PassportStrategy } from "@nestjs/passport";
import { ExtractJwt, Strategy } from "passport-jwt";
import { UserRepository } from "../repository";
import { JwtPayload, defaultUserConfig } from "../helper";
import { instanceToPlain } from "class-transformer";
let config=defaultUserConfig()
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy){
    constructor( protected userRepository:UserRepository){
        super({
            jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
            ignoreExpiration: false,
            secretOrKey: config.jwt.secret
        })
    }
    async validate(payload: JwtPayload){
        let user= this.userRepository.findOne({where:{id:payload.sub}})
        return instanceToPlain(user);
    }
}